package carltechnology.com.mahjonggame.utils.dao.impl;

import carltechnology.com.mahjonggame.base.MyApplication;
import carltechnology.com.mahjonggame.mode.IData;
import carltechnology.com.mahjonggame.utils.Utils;
import carltechnology.com.mahjonggame.utils.dao.IMahjongDao;
import carltechnology.com.mahjonggame.utils.dao.IMahjongSource;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by WZTCM on 2016/6/1.
 */
public class DatabaseSource implements IMahjongSource{
    private IMahjongDao mahjongGameDao;
    private Subscriber<IData> subscriberInfoData;
    private MahjongGameInfoStoreHelper mahjongGameInfoStoreHelper;
    public DatabaseSource() {
        mahjongGameDao = new MahjongGameDao(MyApplication.getContext());
        mahjongGameInfoStoreHelper = new MahjongGameInfoStoreHelper(mahjongGameDao);
    }
    @Override
    public Observable<IData> getMahjongGameInfoSource() {
        return Observable.create(new Observable.OnSubscribe<IData>() {
            @Override
            public void call(Subscriber<? super IData> subscriber) {
                mahjongGameDao.getMahjongGameAllInfo(subscriber);
            }
        }).onBackpressureBuffer(Utils.CONSTANT.ONBACKBUFFER_SIZE.getConstant())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
    @Override
    public MahjongGameInfoStoreHelper getMahjongGameInfoStoreHelper() {
        return mahjongGameInfoStoreHelper;
    }
    @Override
    public Observable<IData> getMahjongGameDetailAllInfoSource(final int id){
        return Observable.create(new Observable.OnSubscribe<IData>() {
            @Override
            public void call(Subscriber<? super IData> subscriber) {
                mahjongGameDao.getMahjongDetailGameAllInfo(subscriber,String.valueOf(id));
            }
        }).onBackpressureBuffer(Utils.CONSTANT.ONBACKBUFFER_SIZE.getConstant())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
}
